/**
	Copyright (c) 2012 ATTi 
	All rights reserved.
	Author: Michael Shu
	Date: 4/3/2012
*/

@isTest
private class MC2PageControllerTester {
	private static string firstContactRecordTypeId   = RecordTypeHelper.GetRecordTypeIdByDevName('Survey_Form__c', 'First_Customer_Contact'); 
	private static string secondContactRecordTypeId  = RecordTypeHelper.GetRecordTypeIdByDevName('Survey_Form__c', 'Second_Customer_Contact');
	private static Set<String> recordTypeIds = new Set<String>{firstContactRecordTypeId, secondContactRecordTypeId};
	private static string CSScustomerAccountRecordTypeId  = RecordTypeHelper.GetRecordTypeIdByDevName('Account', 'CSS_Customer');
	private static string accountId;
	private static Survey_Form__c f;
	private static ApexPages.StandardController sForm;
	private static MC2PageController controller;
		
	private static void init() {
		Account a = new Account();
		a.Name = 'test CSS Customer MC2';
		a.RecordTypeId = CSScustomerAccountRecordTypeId;
		a.CSS_CustomerID__c = '0123456789';
		insert a;
		accountId = a.Id;
	}
	
    static testMethod void testCreateNewForms() {
        init();
        for(String rtId : recordTypeIds){
	        String retURL = '/' + accountId;
	        PageReference pageRef = Page.MC2RedirectPage;
	        Test.setCurrentPage(pageRef); 
	        ApexPages.currentPage().getParameters().put('RecordType', rtId);  
	        ApexPages.currentPage().getParameters().put('retURL', retURL);
	        f = new Survey_Form__c();
	        sForm = new ApexPages.StandardController(f);
	        controller = new MC2PageController(sForm);
	        
	        String nextPageName = '';
	        if(rtId == firstContactRecordTypeId){
	        	nextPageName = 'mc2firstcontactpage';
	        }else if(rtId == secondContactRecordTypeId){
	        	nextPageName = 'mc2secondcontactpage';
	        }
	        String expectedURL = '/apex/' + nextPageName +'?accid=' + accountId + '&RecordType=' + rtId + '&retURL=' + EncodingUtil.urlEncode(retURL,'UTF-8');
	        PageReference nextPage = controller.doRedirect();
	        System.assertEquals(expectedURL, nextPage.getURL());
	        
	        Test.setCurrentPage(nextPage);
	        f = new Survey_Form__c();
	        sForm = new ApexPages.StandardController(f);
	        controller = new MC2PageController(sForm);        
	        PageReference savePage = sForm.save();
	        Survey_Form__c savedf = (Survey_Form__c) sForm.getRecord();
	        System.assertNotEquals(null, sForm.getId());
	        System.assertEquals('/' + sForm.getId(), savePage.getUrl());
	        System.assertEquals('' + savedf.RecordTypeId, rtId);
	        System.assertEquals('' + savedf.Account__c, accountId);
        }
    }
    
    
    
}